import 'dart:io';
import 'dart:typed_data';

import 'package:amoy_ticket/utils/object_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class PhotoViewGalleryScreen extends StatefulWidget {
  final List images;
  final int index;
  final String heroTag;

  PhotoViewGalleryScreen(
      {Key? key, required this.images, this.index = 0, this.heroTag = ''})
      : super(key: key);

  @override
  _PhotoViewGalleryScreenState createState() => _PhotoViewGalleryScreenState();
}

class _PhotoViewGalleryScreenState extends State<PhotoViewGalleryScreen> {
  int currentIndex = 0;
  late PageController _controller;
  @override
  void initState() {
    super.initState();

    _controller = PageController(initialPage: widget.index);
    currentIndex = widget.index;
  }

  _saveScreen(String url) async {
    var response = await Dio()
        .get(url, options: Options(responseType: ResponseType.bytes));
    final result =
        await ImageGallerySaver.saveImage(Uint8List.fromList(response.data));
    // 判断ios还是android,故需要引入 import 'dart:io';
    if (Platform.isIOS) {
      if (result) {
        Fluttertoast.showToast(msg: '成功保存到相册中');
      } else {
        Fluttertoast.showToast(msg: '保存失败');
      }
    } else {
      if (result != null) {
        Fluttertoast.showToast(msg: '成功保存到相册中');
      } else {
        Fluttertoast.showToast(msg: '保存失败');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: <Widget>[
          Positioned(
            top: 0,
            left: 0,
            bottom: 0,
            right: 0,
            child: Container(
                child: PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions(
                  imageProvider:
                      CachedNetworkImageProvider(widget.images[index]),
                  heroAttributes: ObjectUtil.isNotEmpty(widget.heroTag)
                      ? PhotoViewHeroAttributes(tag: widget.heroTag)
                      : null,
                );
              },
              itemCount: widget.images.length,
              backgroundDecoration: null,
              pageController: _controller,
              enableRotation: false,
              onPageChanged: (index) {
                setState(() {
                  currentIndex = index;
                });
              },
            )),
          ),
          Positioned(
            //图片index显示
            top: MediaQuery.of(context).padding.top + 15,
            width: MediaQuery.of(context).size.width,
            child: Center(
              child: Text("${currentIndex + 1}/${widget.images.length}",
                  style: TextStyle(color: Colors.white, fontSize: 16)),
            ),
          ),
          Positioned(
            //右上角关闭按钮
            right: 10,
            top: MediaQuery.of(context).padding.top,
            child: IconButton(
              icon: Icon(
                Icons.close,
                size: 30,
                color: Colors.white,
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ),
          Positioned(
            //左下角保存图片
            bottom: MediaQuery.of(context).padding.bottom + 16,
            child: TextButton(
              child: Text(
                '保存图片',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
              onPressed: () {
                _saveScreen(widget.images[currentIndex]);
              },
            ),
          )
        ],
      ),
    );
  }
}
